﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

using Infrastructure.Core.IOC;
using Infrastructure.Utility;
using SMG.ServiceContract.System;

namespace ManageForm
{
    public partial class ProgramMonitorSettingForm : Form
    {
        private readonly String programMonitorSettingKey = "ProgramMonitorSetting";
        ICommonService commonService = ServiceLocator.Instance.Resolve<ICommonService>();
        
        public ProgramMonitorSettingForm()
        {
            InitializeComponent();
        }

        private void ProgramMonitorSettingForm_Load(object sender, EventArgs e)
        {
            LoadSettngData();
        }

        private void LoadSettngData() {
            try
            {
                var dict = commonService.GetDictByKey(programMonitorSettingKey);
                if (dict != null) {
                    var setting = SerializeUtility.JsonDeserialize<MonitorSetting>(dict.DictValue);
                    if (setting != null) {
                        this.ckbNewFile.Checked = false;
                        this.ckbUpdateFile.Checked = false;
                        this.ckbDeleteFile.Checked = false;

                        if (setting.MonitorType.IndexOf("Create") >= 0) this.ckbNewFile.Checked = true;
                        if (setting.MonitorType.IndexOf("Update") >= 0) this.ckbUpdateFile.Checked = true;
                        if (setting.MonitorType.IndexOf("Delete") >= 0) this.ckbDeleteFile.Checked = true;

                        this.txtMonitorPath.Text = setting.MonitorPath;

                        var fileTypeArray = setting.Filter.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                        foreach (ListViewItem item in this.listViewMonitorFileType.Items) {
                            if (setting.Filter.IndexOf(item.Text) >= 0) item.Checked = true;
                        }
                    }
                }
            }
            catch (Exception ex) { 
                
            }
        }

        private void toolStripButtonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            String monitorType = String.Empty, monitorPath = String.Empty, monitorFileType = String.Empty;
            try
            {
                if (ckbNewFile.Checked) monitorType += "Create,";
                if (ckbUpdateFile.Checked) monitorType += "Update,";
                if (ckbDeleteFile.Checked) monitorType += "Delete,";
                if (String.IsNullOrEmpty(monitorType))
                {
                    MessageBox.Show("请选择一种监控类型");
                    return;
                }
                monitorPath = this.txtMonitorPath.Text;
                if (String.IsNullOrEmpty(monitorPath))
                {
                    MessageBox.Show("请设置监视文件目录");
                    return;
                }
                if (!Directory.Exists(monitorPath))
                {
                    MessageBox.Show("无效的监视文件目录路径");
                    return;
                }
                if (this.listViewMonitorFileType.CheckedItems.Count <= 0)
                {
                    MessageBox.Show("请设置监视文件类型");
                    return;
                }
                foreach (ListViewItem item in this.listViewMonitorFileType.CheckedItems)
                {
                    monitorFileType += item.Text + ",";
                }
                if (String.IsNullOrEmpty(monitorFileType))
                {
                    MessageBox.Show("请设置监视文件类型");
                    return;
                }
                var isnew = false;
                var dictDto = commonService.GetDictByKey(programMonitorSettingKey);
                if (dictDto == null)
                {
                    isnew = true;
                    dictDto = new SMG.Application.DTO.System.SysDictDto();
                }

                var setting = new MonitorSetting();
                setting.MonitorPath = this.txtMonitorPath.Text;
                setting.Filter = monitorFileType;
                setting.MonitorType = monitorType;

                dictDto.DictKey = programMonitorSettingKey;
                dictDto.DictValue = SerializeUtility.JsonSerialize<MonitorSetting>(setting);
                dictDto.UpdateTime = DateTime.Now;
                var result = false;
                if (isnew) result = commonService.AddDictItem(dictDto);
                else result = commonService.UpdateDictItem(dictDto);

                if (!result) {
                    MessageBox.Show("数据修改失败");
                    return;
                }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("异常:{0}", ex.Message));
            }
            finally { 
                
            }
            
        }

        private void btnSelectMonitorPath_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                this.txtMonitorPath.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }
    }
}
